package kr.co.cherryt.view;

import kr.co.cherryt.activity.CherryTActivity;
import kr.co.cherryt.cherryt.R;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

//hs-TODO// convertView를 사용하도록 수정해보자. (성능을 위해 20개의 배열을 사용한 것을 고려해볼 것)
//hs-TODO// viewHolder의 개념을 적용하도록 수정해보자.

public class AdGallery extends BaseAdapter {
    private Context context;
    static public ImageView [] imageArray = new ImageView [50];

    public AdGallery(Context c) {
        context = c;
        for( int i=0; i<50; i++ ) imageArray[i] = null;
    }

	public int getCount() {
        return CherryTActivity.adInfoArray.size();
    }

    public Integer getItem(int position) {
        return position;
    }

	public void displayAdText(int index)
	{
		//Log.d("cherryt", "DisplayAdText: " + CherryTActivity.adInfoArray.get(index).getPoint() );
		
		((TextView)((Activity) CherryTActivity.context).findViewById(R.id.ad_title)).setText( CherryTActivity.adInfoArray.get(index).getTitle() );
		((TextView)((Activity) CherryTActivity.context).findViewById(R.id.ad_writer)).setText( CherryTActivity.adInfoArray.get(index).getWriter() );
		((TextView)((Activity) CherryTActivity.context).findViewById(R.id.ad_cate)).setText( CherryTActivity.adInfoArray.get(index).getAdCate() );
		
		if( CherryTActivity.bPersonalAd ) 
		{
			if( CherryTActivity.adInfoArray.get(index).getLoc() != null )
			{
				((TextView)((Activity) CherryTActivity.context).findViewById(R.id.ad_point)).setText( CherryTActivity.adInfoArray.get(index).getLoc() );
			}
		}
		else
		{
			((TextView)((Activity) CherryTActivity.context).findViewById(R.id.ad_point)).setText( CherryTActivity.adInfoArray.get(index).getPoint() );
		}
		((TextView)((Activity) CherryTActivity.context).findViewById(R.id.ad_subcon)).setText( CherryTActivity.adInfoArray.get(index).getSubCon() );
    }
	
    public long getItemId(int position) {
    	//Log.d("cherryt", "AdGallery - getItemId: " + position);
    	displayAdText(position);
    	CherryTActivity.mAdPosition = position;

    	// 갤러리의 좌측 및 우측에 나오는 버튼을 숨겼다 보였다 조절한다.
//    	if( getCount() > 0 ) {
//    		if( CherryTActivity.mGallery.getSelectedItemPosition() == 0) {
//    			((Activity) CherryTActivity.context).findViewById(R.id.btnGalleryLeft).setVisibility(View.INVISIBLE);
//    			
//    			if( getCount() > 1 ) { 
//    				((Activity) CherryTActivity.context).findViewById(R.id.btnGalleryRight).setVisibility(View.VISIBLE);
//    			}
//    			else {
//    				((Activity) CherryTActivity.context).findViewById(R.id.btnGalleryRight).setVisibility(View.INVISIBLE);
//    			}
//    		}
//    		else if( CherryTActivity.mGallery.getSelectedItemPosition() > 0) {
//    			((Activity) CherryTActivity.context).findViewById(R.id.btnGalleryLeft).setVisibility(View.VISIBLE);
//    			
//        		if( CherryTActivity.mGallery.getSelectedItemPosition() < getCount()-1 ) {
//        			((Activity) CherryTActivity.context).findViewById(R.id.btnGalleryRight).setVisibility(View.VISIBLE);
//        		}
//        		else {
//        			((Activity) CherryTActivity.context).findViewById(R.id.btnGalleryRight).setVisibility(View.INVISIBLE);
//        		}
//    		}
//    	}

		return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
		//Log.d("cherryt", "AdGallery - [## getView ##]: "+position);
		return imageArray[position];
	}
}
